package com.gdpu.pbss.controller;

import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gdpu.pbss.common.HoneyLogs;
import com.gdpu.pbss.common.LogType;
import com.gdpu.pbss.common.Result;
import com.gdpu.pbss.pojo.Pb;
import com.gdpu.pbss.pojo.Pbss;
import com.gdpu.pbss.service.PbService;
import com.gdpu.pbss.service.PbssService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/Pb")
public class PbController {


    @Autowired
    private PbService pbService;

    @Autowired
    private PbssService pbssService;


    @Autowired
    private RedisTemplate redisTemplate;

    public static final String PB_KEY="selectPbPage";


    @GetMapping("/page")
    public Result selectPbPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize,
                                 @RequestParam(defaultValue = "") Integer pbId,
                                 @RequestParam(defaultValue = "")  Integer pbssId
    )
    {




        IPage<Pb> page=new Page<>(pageNum,pageSize);
        QueryWrapper<Pb> qw =new QueryWrapper<>();

        qw.eq(pbssId!=null,"pbss_id",pbssId);
        qw.eq(pbId!=null,"pb_id",pbId);
        qw.orderByDesc("pb_id");

        return Result.success(pbService.page(page,qw));



    }

    @PostMapping("/update")
    public Result updatePb(@RequestBody Pb pb){

        if(pb.getPbStatus()){
            QueryWrapper<Pbss> qw=new QueryWrapper<>();
            qw.eq("pbss_name",pb.getPbssName());
            Pbss one = pbssService.getOne(qw);
            one.setSendCount(one.getSendCount()+1);
        }
        return Result.success(pbService.updateById(pb));
    }

    @HoneyLogs(operation = "充电宝管理",type = LogType.ADD)
    @PostMapping("/save")
    public Result savePb(@RequestBody Pb pb){

//        redisTemplate.delete(PB_KEY);
        if (pb.getPbId()==null){
            QueryWrapper<Pbss> qw=new QueryWrapper<>();
            qw.eq("pbss_name",pb.getPbssName());
            Pbss one = pbssService.getOne(qw);
            one.setPbCount(one.getPbCount()+1);
            pbssService.updateById(one);

            pb.setPbssId(one.getPbssId());

            return Result.success(pbService.save(pb));
        }else {
            QueryWrapper<Pbss> qw=new QueryWrapper<>();
            qw.eq("pbss_id",pb.getPbssId());
            Pbss oldone = pbssService.getOne(qw);
            oldone.setPbCount(oldone.getPbCount()-1);
            pbssService.updateById(oldone);


            QueryWrapper<Pbss> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("pbss_name",pb.getPbssName());
            Pbss newone = pbssService.getOne(queryWrapper);
            newone.setPbCount(newone.getPbCount()+1);
            if (pb.getPbStatus()){
                newone.setSendCount(newone.getSendCount()+1);
            }
            pbssService.updateById(newone);



            pb.setPbssId(newone.getPbssId());
           
            return Result.success(pbService.updateById(pb));
        }
    }


    @HoneyLogs(operation = "充电宝管理",type = LogType.DELETE)
    @DeleteMapping("/{pbId}")
    public Result delPb(@PathVariable("pbId") Integer pbId){
        QueryWrapper<Pb> qw=new QueryWrapper<>();
        qw.eq("pb_id",pbId);
        Pb one = pbService.getOne(qw);

        QueryWrapper<Pbss> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("pbss_id",one.getPbssId());
        Pbss pbss1 = pbssService.getOne(queryWrapper);
        pbss1.setPbCount(pbss1.getPbCount()-1);
        pbssService.updateById(pbss1);


        pbService.removeById(pbId);
        return Result.success();
    }

    @HoneyLogs(operation = "充电宝管理",type = LogType.BATCH_DELETE)
    @PostMapping("/del/batch")
    public Result delBatchPb(@RequestBody List<Integer> ids){
        QueryWrapper<Pb> qw=new QueryWrapper<>();
        qw.in("pb_id",ids);
        List<Pb> pbList = pbService.list(qw);
        for (Pb pb : pbList) {
            QueryWrapper<Pbss> pbssQueryWrapper=new QueryWrapper<>();
            pbssQueryWrapper.eq("pbss_id",pb.getPbssId());
            Pbss pbssServiceOne = pbssService.getOne(pbssQueryWrapper);
            pbssServiceOne.setPbCount(pbssServiceOne.getPbCount()-1);
            pbssService.updateById(pbssServiceOne);

            pbService.removeById(pb.getPbId());

        }
        return Result.success();
    }


    @GetMapping("/pbName")
    public Result getPbName(){

        QueryWrapper<Pb> qw=new QueryWrapper<>();
        qw.eq("enable",true);

        List<Pb> pbs = pbService.list(qw);

        List<String> nameList=new ArrayList<>();
        for (Pb pb : pbs) {
          nameList.add(pb.getPbName()) ;
        }
        return Result.success(nameList);
    }



}
